package top.chukongxiang.project.base.oauth2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.cas.authentication.CasAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;
import org.springframework.stereotype.Component;
import top.chukongxiang.project.base.oauth2.provider.MyDaoAuthenticationProvider;
import top.chukongxiang.project.base.oauth2.provider.cas.CasAuthenticationUserDetailsServiceImpl;

import java.util.Arrays;

/**
 * Spring Security
 */
@Configuration
@Component("authWebSecurityConfig")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
public class AuthWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    CasAuthenticationUserDetailsServiceImpl casAuthenticationUserDetailsServiceImpl;

    @Autowired
    CasAuthenticationProvider casAuthenticationProvider;

    @Autowired
    MyDaoAuthenticationProvider daoAuthenticationProvider;


    /**
     * 授权认证器配置
     * @param auth
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // oauth
//        auth.authenticationProvider(casAuthenticationProvider)
//                .authenticationProvider(daoAuthenticationProvider)
//                .inMemoryAuthentication();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManager() throws Exception {
        // oauth
        return new ProviderManager(Arrays.asList(casAuthenticationProvider, daoAuthenticationProvider));

//        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) {
//        http.authenticationProvider(daoAuthenticationProvider)
//                .authenticationProvider(casAuthenticationProvider)
//                ;
        //
//        http.authorizeRequests()
//                .anyRequest().authenticated() //所有请求都需要通过认证
//                .and()
//                .httpBasic() //Basic登录
//                .and()
//                .csrf().disable(); //关跨域保护
    }

    @Bean
    public HttpFirewall allowUrlSemicolonHttpFirewall() {
        StrictHttpFirewall firewall = new StrictHttpFirewall();
        firewall.setAllowSemicolon(true);
        firewall.setAllowUrlEncodedDoubleSlash(true);
        return firewall;
    }


}
